package com.pompom.workers;

import nl.ansuz.android.steam.dota.response.match.GetMatchDetailsResponse;
import nl.ansuz.android.steam.dota.response.match.GetMatchHistoryBySequenceNumResponse;

import com.pompom.matchcrawler.MatchPersistor;

public class MatchProcessorThread implements Runnable{

	GetMatchHistoryBySequenceNumResponse matchHistory;
	MatchPersistor mpersistor;
	
	
	public MatchProcessorThread(
			GetMatchHistoryBySequenceNumResponse matchHistory,
			MatchPersistor mpersistor) {
		super();
		this.matchHistory = matchHistory;
		this.mpersistor = mpersistor;
	}


	@Override
	public void run() {

		processMatches();
		
	}

	
	/**
	 * 
	 * @param matchHistory
	 * @param lastMatch
	 * @param accountid
	 * @param bound
	 * @return true se ja chegou no bound - parar de processar
	 */
	private boolean processMatches(  )
	{

		//System.out.println("latest starting processMatches lastmatch "  );
		boolean hasNewRecords=false;
		long antes = System.currentTimeMillis();
		for (int i=0;i<matchHistory.matches.length;i++)
		{
			GetMatchDetailsResponse matchVO = matchHistory.matches[i]; 

			//System.out.println("--processing match "+matchVO.matchId+" :: "+matchVO.startTime);


			if (!mpersistor.persist(matchVO))
				hasNewRecords=true;



			// MatchDetailWorker.putMatchInWorkQueue(matchVO.matchId, TOMatchWorkQueue.PRIORITY_LOW);
			// System.out.println("matchWK "+matchVO.matchId);

		}
		mpersistor.flushAll();
		antes = System.currentTimeMillis()-antes;
		System.out.println("t3: "+antes);
		System.out.println("Persisting "+matchHistory.matches[0].matchId );




		return hasNewRecords;
	}
}
